package com.defysoft.waukids.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.defysoft.waukids.Data_Type.BC_NOTICE;
import com.defysoft.waukids.PublicConstant;
import com.defysoft.waukids.R;
import com.defysoft.waukids.notice.Notice_Detail_A;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

public class Adapter_Notice_L extends ArrayAdapter<String>{
	public ArrayList<BC_NOTICE> list;
	LayoutInflater layoutInflater;
	FragmentActivity fragmentActivity;
	
	//이미지 세팅
	Context context;
	DisplayImageOptions options; 
	ImageLoader imageLoader;
	public Adapter_Notice_L(FragmentActivity fragmentActivity, int resource,ArrayList<BC_NOTICE> list,
			LayoutInflater layoutInflater) {
		super(fragmentActivity, resource);
		this.fragmentActivity=fragmentActivity;
		this.layoutInflater=layoutInflater;
		this.list = list;
		
		//이미지 세팅.
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				fragmentActivity).threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.writeDebugLogs() // Remove for release app
				.build();
		// Initialize ImageLoader with configuration.
		ImageLoader.getInstance().init(config);
		imageLoader = ImageLoader.getInstance();
		imageLoader.clearDiskCache();
		imageLoader.clearMemoryCache();

		options = new DisplayImageOptions.Builder().cacheInMemory(true)
				.showImageOnFail(R.drawable.logo)
				.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
				.considerExifParams(true).build();
	}

@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Notice_List_Holder holder;
		if(convertView == null){
			holder = new Notice_List_Holder();
			convertView = layoutInflater.inflate(R.layout.adapter_notice_l, null);
			holder.tv1=(TextView)convertView.findViewById(R.id.NOTICE_NAME);
			holder.tv2=(TextView)convertView.findViewById(R.id.NOTICE_SUB);
			holder.tv3=(TextView)convertView.findViewById(R.id.NOTICE_DATE);
			holder.iv = (ImageView)convertView.findViewById(R.id.NOTICE_IMG);
			
			
			convertView.setTag(holder);
		}else{
			holder = (Notice_List_Holder)convertView.getTag();
		}
		
		/*for (int i = 0; i < list.size(); i++) {
			holder.iv.setVisibility(View.VISIBLE);
			if (list.get(i).Not_Photo.length() <5) {
				holder.iv.setVisibility(View.GONE);
			} 
		}*/
		holder.iv.setVisibility(View.GONE);
		if (list.get(position).Not_Photo.length() > 5) {
			holder.iv.setVisibility(View.VISIBLE);
			imageLoader.displayImage(PublicConstant.FTP_NOTICE_PHOTO+list.get(position).Not_Photo, holder.iv, options, null);
		}
		
		holder.tv1.setText(list.get(position).Not_Name);
		holder.tv2.setText(list.get(position).Not_Sub);
		holder.tv3.setText(list.get(position).Not_Date);
		
		final int pos = position;
		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				fragmentActivity.startActivityForResult(new Intent(getContext(), Notice_Detail_A.class).putExtra("notice_code", list.get(pos).PK_Not_Code+""), 0);
/*				getContext().startActivity(new Intent(getContext(), Notice_Detail_A.class).putExtra("notice_code", list.get(pos).PK_Not_Code+""));
*/			}
		});
		return convertView;
		
	}

class Notice_List_Holder{
	TextView tv1;
	TextView tv2;
	TextView tv3;
	ImageView iv;
}
	
}//CLASS_END
